const router = require('koa-router')()
const DB = require('../other/DB');

router.prefix('/api/home')
// 获取项目列表
router.get('/topic/list', async ctx => {
    ctx.body = await DB.query(`SELECT * FROM topic WHERE desable = 0 ORDER BY create_time`)
});

// 获取答题情况
router.get('/inspect/list', async ctx => {
    ctx.body = await DB.query(`SELECT * FROM inspect WHERE user_id = ? ORDER BY create_time`, [ctx.userInfo.userName])
});


router.post('/inspect/otheruser/answer', async ctx => {
    console.log(ctx.userInfo.userName, ctx.request.body.id);
    ctx.body = await DB.query(`SELECT
        i.code_content \`Code\`,
        i.id,
        i.inspectA,
        i.inspectB,
        i.inspectC,
        i.inspectD,
        i.inspectE,
        i.total_points total,
        i.update_time update_time,
        i.draft,
        i.topic_id,
        u.nickname,
        u.username 
    FROM
        ( SELECT * FROM \`user\` WHERE username <> ? AND permission = 'user' ) u
        LEFT JOIN ( SELECT * FROM inspect WHERE topic_id = ? ) i ON u.username = i.user_id 
    WHERE
        u.desable = 0 
        AND i.draft = 1` , [ctx.userInfo.userName, ctx.request.body.id]);
});



module.exports = router